﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Sunboxsoft.WinForm.Shell.Components;
using DevExpress.XtraEditors;

namespace Sunboxsoft.WinForm.Shell
{
    public partial class PopupForm : Form, IViewContainer
    {
        public const int ActionBarHeight = 50;
        /// <summary>
        /// 是通过右上角的关闭按钮来关闭的
        /// </summary>
        public bool IsClosingByXButton { get; set; }

        private MaskLayer _ctlMaskLayer;
        private int _maskLayerRetain;

        public PopupForm()
        {
            IsClosingByXButton = true;

            InitializeComponent();
        }


        public void RemoveAllView()
        {
            ContentContainer.Controls.Clear();
        }

        public void AddView(object view)
        {
            for (int index = 0; index < ContentContainer.Controls.Count; index++)
            {
                if (ContentContainer.Controls[index] == view)
                {
                    ContentContainer.Controls[index].Visible = true;
                    return;
                }
            }
            ContentContainer.Controls.Add((Control)view);
            ShowDebugText(view.GetType().FullName);
        }

        public void RemoveView(object view)
        {
            ContentContainer.Controls.Remove((Control)view);
        }

        public object GetLastView()
        {
            if (ContentContainer.Controls.Count > 0)
            {
                return ContentContainer.Controls[ContentContainer.Controls.Count - 1];
            }
            return null;
        }
        public void HideCurrentView()
        {
            if (ContentContainer.Controls.Count > 0)
            {
                ContentContainer.Controls[ContentContainer.Controls.Count - 1].Visible = false;
            }
        }
        public void ShowCurrentView()
        {
            if (ContentContainer.Controls.Count > 0)
            {
                ContentContainer.Controls[ContentContainer.Controls.Count - 1].Visible = true;

                ShowDebugText(ContentContainer.Controls[ContentContainer.Controls.Count - 1].GetType().FullName);
            }
        }

        public void RemoveAllActionButton()
        {
            ActionContainer.Controls.Clear();
        }

        public void AddActionButton(SimpleButton button)
        {
            ActionContainer.Controls.Add(button);
        }

        #region IViewContainer 成员

        public void ShowMask(string text)
        {
            this.BeginInvoke(new Action(() =>
            {
                if (_ctlMaskLayer != null)
                {
                    _ctlMaskLayer.SetText(text);
                }
                else
                {
                    _ctlMaskLayer = new MaskLayer();
                    _ctlMaskLayer.Dock = DockStyle.Fill;
                    this.Controls.Add(_ctlMaskLayer);
                    _ctlMaskLayer.BringToFront();
                    _ctlMaskLayer.SetText(text);
                }
                _maskLayerRetain++;
            }));
        }

        public void HideMask()
        {
            this.BeginInvoke(new Action(() =>
            {
                _maskLayerRetain--;
                if (_ctlMaskLayer != null)
                {
                    if (_maskLayerRetain == 0)
                    {
                        this.Controls.Remove(_ctlMaskLayer);
                        _ctlMaskLayer = null;
                    }
                }
            }));
        }

        public void ShowAction(string name, bool isShow)
        {
            for (int index = 0; index < ActionContainer.Controls.Count; index++)
            {
                if (string.Equals(ActionContainer.Controls[index].Name, name))
                {
                    if (ActionContainer.Controls[index].Visible != isShow)
                    {
                        ActionContainer.Controls[index].Visible = isShow;
                        RelayoutActionButton();
                    }
                    break;
                }
            }
        }

        public void EnableAction(string name, bool isEnable)
        {
            for (int index = 0; index < ActionContainer.Controls.Count; index++)
            {
                if (string.Equals(ActionContainer.Controls[index].Name, name))
                {
                    ActionContainer.Controls[index].Enabled = isEnable;
                    break;
                }
            }
        }

        #endregion

        public void RelayoutActionButton()
        {
            int left = 10;
            for (int index = 0; index < ActionContainer.Controls.Count; index++)
            {
                SimpleButton button = ActionContainer.Controls[index] as SimpleButton;
                if (button.Visible)
                {
                    button.Location = new System.Drawing.Point(left, 5);

                    left += button.Width;
                    left += 10;
                }
            }
        }

        public void ShowDebugText(string text)
        {
//#if DEBUG
//            _ctlLabelDebug.Text = text;
//#endif
        }
    }
}
